package cn.admobiletop.adsuyi.adapter.toutiao.data;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTSplashAd;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiSplashAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.CustomDownloadListener;
import cn.admobiletop.adsuyi.util.ADSuyiLogUtil;

/**
 * @author ciba
 * @description 开屏广告内容对象
 * @date 2020/4/3
 */
public class SplashAdInfo extends BaseAdInfo<ADSuyiSplashAdListener, TTSplashAd> {

    private Handler handler = new Handler(Looper.getMainLooper());

    public SplashAdInfo(String platformPosId) {
        super(platformPosId);
    }

    @Override
    public void setAdapterAdInfo(TTSplashAd ttSplashAd) {
        super.setAdapterAdInfo(ttSplashAd);

        setSplashInteractionListener();
    }

    @Override
    public void onCloseClick(View view) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdClose(SplashAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        setAdapterAdInfo(null);

        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    /**
     * 渲染开屏广告和设置广告监听
     */
    private void setSplashInteractionListener() {
        if (getAdapterAdInfo() != null) {
            // 如果是下载类型广告，绑定下载监听
            if (TTAdConstant.INTERACTION_TYPE_DOWNLOAD == getAdapterAdInfo().getInteractionType()) {
                getAdapterAdInfo().setDownloadListener(new CustomDownloadListener());
            }

            // 设置开屏广告监听（点击、曝光、跳过等）
            getAdapterAdInfo().setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                @Override
                public void onAdClicked(View view, int i) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null) {
                                    getAdListener().onAdClick(SplashAdInfo.this);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onAdShow(View view, int i) {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null) {
                                    getAdListener().onAdExpose(SplashAdInfo.this);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onAdSkip() {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (getAdListener() != null) {
                                    getAdListener().onAdClose(SplashAdInfo.this);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onAdTimeOver() {
                    if (handler != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                // setNotAllowSdkCountdown 设置了这个之后没有倒计时按钮并且没有该回调
                                onCloseClick(null);
                            }
                        });
                    }
                }
            });
        }
    }
}
